package com.galaxy.device.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.galaxy.common.core.exception.ServiceException;
import com.galaxy.common.core.utils.MapstructUtils;
import com.galaxy.common.core.utils.StringUtils;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.device.domain.DeviceMachineType;
import com.galaxy.device.domain.bo.DeviceMachineTypeBo;
import com.galaxy.device.domain.convert.DeviceMachineTypeConvert;
import com.galaxy.device.domain.vo.DeviceMachineTypeVo;
import com.galaxy.device.mapper.DeviceMachineTypeMapper;
import com.galaxy.device.service.IDeviceMachineTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 机器类型Service业务层处理
 */
@RequiredArgsConstructor
@Service
public class DeviceMachineTypeServiceImpl implements IDeviceMachineTypeService {

    private final DeviceMachineTypeMapper machineTypeMapper;

    @DS("slave")
    @Override
    public TableDataInfo<DeviceMachineTypeVo> queryPageList(DeviceMachineTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DeviceMachineType> lqw = buildQueryWrapper(bo);
        IPage<DeviceMachineTypeVo> page = machineTypeMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    @DS("slave")
    @Override
    public List<DeviceMachineTypeVo> queryList(DeviceMachineTypeBo bo) {
        LambdaQueryWrapper<DeviceMachineType> lqw = buildQueryWrapper(bo);
        return machineTypeMapper.selectVoList(lqw);
    }

    @DS("slave")
    @Override
    public DeviceMachineTypeVo getInfo(Long id) {
        DeviceMachineType machineType = machineTypeMapper.selectById(id);
        if (machineType == null) {
            throw new ServiceException("机器类型信息不存在");
        }
        return DeviceMachineTypeConvert.INSTANCE.convertVo(machineType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(DeviceMachineTypeBo bo) {
        DeviceMachineType machineType = DeviceMachineTypeConvert.INSTANCE.convert(bo);
        machineTypeMapper.insert(machineType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DeviceMachineTypeBo bo) {
        DeviceMachineType machineType = machineTypeMapper.selectById(bo.getId());
        if (machineType == null) {
            throw new ServiceException("机器类型信息不存在");
        }
        MapstructUtils.convert(bo, machineType);
        machineTypeMapper.updateById(machineType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        machineTypeMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            machineTypeMapper.deleteById(id);
        }
    }

    private LambdaQueryWrapper<DeviceMachineType> buildQueryWrapper(DeviceMachineTypeBo bo) {
        LambdaQueryWrapper<DeviceMachineType> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(bo.getTypeCode()), DeviceMachineType::getTypeCode, bo.getTypeCode());
        lqw.like(StringUtils.isNotBlank(bo.getTypeName()), DeviceMachineType::getTypeName, bo.getTypeName());
        lqw.eq(bo.getStatus() != null, DeviceMachineType::getStatus, bo.getStatus());
        return lqw;
    }
}
